package com.h.forum.services.impl;

import com.h.forum.common.enums.ResultCode;
import com.h.forum.dao.BoardMapper;
import com.h.forum.exception.ApplicationException;
import com.h.forum.pojo.model.Board;
import com.h.forum.pojo.response.AppResult;
import com.h.forum.pojo.response.BoardInfoResponse;
import com.h.forum.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    /**
     * 查询前N条有效的版块记录
     * @param num
     * @return
     */
    @Override
    public BoardInfoResponse selectByNum(Integer num) {
        // 1、非空校验
        if (num <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2、从数据库中查询有效的板块信息
        List<Board> boards = boardMapper.selectByNum(num);
        BoardInfoResponse response = new BoardInfoResponse();
        response.setBoardList(boards);

        return response;
    }

    /**
     * 更新板块中的帖子个数进行+1操作
     * @param id
     */
    @Override
    public void addOneArticleCountById(Long id) {
        // 1、校验信息
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        // 2、查询对应的板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 3、判断这个板块是否存在
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        // 4、创建一个新的板块根据需要更新的数据
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        // 5、根据信息，进行更新数据库中的数据
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        // 6、判断受影响的行数是否是1
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString()  + ", 受影响的行数不等于 1 .");
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        // 7、更新成功
    }

    /**
     * 根据Id，查询板块信息
     * @param id
     * @return
     */
    @Override
    public Board selectById(Long id) {
        // 1、非空校验
        if (id == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 2、查询
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        return board;
    }

    /**
     * 更新板块中的帖子个数进行-1操作
     * @param id
     */
    @Override
    public void subOneArticleCountById(Long id) {
        // 1、校验信息
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 2、查询对应的板块信息
        Board board = boardMapper.selectByPrimaryKey(id);
        // 3、判断这个板块是否存在
        if (board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        // 4、创建一个新的板块根据需要更新的数据
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);

        // 5、根据信息，进行更新数据库中的数据
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        // 6、判断受影响的行数是否是1
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString()  + ", 受影响的行数不等于 1 .");
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        // 7、更新成功
    }
}
